#ifndef __BITBAND_H_
#define __BITBAND_H_

#include "stm32f10x.h"

// 计算位段别名区字地址
// bit_word_addr = bit_band_base + (byte_offset × 32) + (bit_number × 4)
#define BIT_WORD_ADDR(bit_band_base, byte_offset, bit_number) \
	((bit_band_base) + ((byte_offset) * 32) + ((bit_number) * 4))

// 计算位带区某个位对应别名区字地址
// 如：ALIAS_WORD_ADDR(&GPIOC->ODR, 9)
#define ALIAS_WORD_ADDR(addr, bit_number) \
	BIT_WORD_ADDR(((addr) & 0xF0000000) + 0x02000000, (addr) & 0x000FFFFF, bit_number)

// 定义各个 GPIO 端口的输入和输出
#define PAin_(bit_number)  (*((__IO uint32_t *) ALIAS_WORD_ADDR((uint32_t)&GPIOA->IDR, bit_number)))
#define PBin_(bit_number)  (*((__IO uint32_t *) ALIAS_WORD_ADDR((uint32_t)&GPIOB->IDR, bit_number)))
#define PCin_(bit_number)  (*((__IO uint32_t *) ALIAS_WORD_ADDR((uint32_t)&GPIOC->IDR, bit_number)))
#define PDin_(bit_number)  (*((__IO uint32_t *) ALIAS_WORD_ADDR((uint32_t)&GPIOD->IDR, bit_number)))
#define PEin_(bit_number)  (*((__IO uint32_t *) ALIAS_WORD_ADDR((uint32_t)&GPIOE->IDR, bit_number)))
#define PFin_(bit_number)  (*((__IO uint32_t *) ALIAS_WORD_ADDR((uint32_t)&GPIOF->IDR, bit_number)))
#define PGin_(bit_number)  (*((__IO uint32_t *) ALIAS_WORD_ADDR((uint32_t)&GPIOG->IDR, bit_number)))

#define PAout_(bit_number) (*((__IO uint32_t *) ALIAS_WORD_ADDR((uint32_t)&GPIOA->ODR, bit_number)))
#define PBout_(bit_number) (*((__IO uint32_t *) ALIAS_WORD_ADDR((uint32_t)&GPIOB->ODR, bit_number)))
#define PCout_(bit_number) (*((__IO uint32_t *) ALIAS_WORD_ADDR((uint32_t)&GPIOC->ODR, bit_number)))
#define PDout_(bit_number) (*((__IO uint32_t *) ALIAS_WORD_ADDR((uint32_t)&GPIOD->ODR, bit_number)))
#define PEout_(bit_number) (*((__IO uint32_t *) ALIAS_WORD_ADDR((uint32_t)&GPIOE->ODR, bit_number)))
#define PFout_(bit_number) (*((__IO uint32_t *) ALIAS_WORD_ADDR((uint32_t)&GPIOF->ODR, bit_number)))
#define PGout_(bit_number) (*((__IO uint32_t *) ALIAS_WORD_ADDR((uint32_t)&GPIOG->ODR, bit_number)))

#endif  // __BITBAND_H_
